/**
 * 通知工具类
 * Notification Utility Class
 */

class NotificationManager {
  constructor() {
    this.container = document.getElementById('notification');
    this.icon = document.getElementById('notification-icon');
    this.message = document.getElementById('notification-message');
    this.closeBtn = document.getElementById('notification-close');
    
    this.init();
  }

  init() {
    // 绑定关闭按钮事件
    if (this.closeBtn) {
      this.closeBtn.addEventListener('click', () => {
        this.hide();
      });
    }
  }

  /**
   * 显示通知
   */
  show(message, type = 'info', duration = 5000) {
    if (!this.container) return;

    // 设置消息内容
    this.message.textContent = message;

    // 设置图标和样式
    this.setTypeStyle(type);

    // 显示通知
    this.container.classList.remove('hidden');

    // 自动隐藏
    if (duration > 0) {
      setTimeout(() => {
        this.hide();
      }, duration);
    }
  }

  /**
   * 隐藏通知
   */
  hide() {
    if (this.container) {
      this.container.classList.add('hidden');
    }
  }

  /**
   * 设置通知类型样式
   */
  setTypeStyle(type) {
    // 清除之前的样式
    this.container.className = 'fixed top-4 right-4 z-40';
    this.icon.className = 'flex-shrink-0';

    switch (type) {
      case 'success':
        this.container.classList.add('text-green-800');
        this.icon.innerHTML = '<i class="fa fa-check-circle text-green-500 text-lg"></i>';
        break;
      case 'error':
        this.container.classList.add('text-red-800');
        this.icon.innerHTML = '<i class="fa fa-exclamation-circle text-red-500 text-lg"></i>';
        break;
      case 'warning':
        this.container.classList.add('text-yellow-800');
        this.icon.innerHTML = '<i class="fa fa-exclamation-triangle text-yellow-500 text-lg"></i>';
        break;
      default: // info
        this.container.classList.add('text-blue-800');
        this.icon.innerHTML = '<i class="fa fa-info-circle text-blue-500 text-lg"></i>';
        break;
    }
  }

  /**
   * 显示成功消息
   */
  success(message, duration = 3000) {
    this.show(message, 'success', duration);
  }

  /**
   * 显示错误消息
   */
  error(message, duration = 5000) {
    this.show(message, 'error', duration);
  }

  /**
   * 显示警告消息
   */
  warning(message, duration = 4000) {
    this.show(message, 'warning', duration);
  }

  /**
   * 显示信息消息
   */
  info(message, duration = 3000) {
    this.show(message, 'info', duration);
  }
}

// 创建全局通知管理器实例
window.notification = new NotificationManager();